//
//  SettingViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/10/4.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "SettingViewController.h"
#import <JPUSHService.h>
#import "EditLoginMobileController.h"

@interface SettingViewController ()

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNaviWith:@"设置"];
    self.view.backgroundColor = UIColorFromHex(0xF7F7F7);
    [self setMainView];
}

- (void)setMainView
{
    UIView *headView = [UIView new];
    if (@available(iOS 11.0, *)) {
        headView.frame = CGRectMake(0, TopBarHeight_xs+10, SCREENWIDTH, 112+56);
    } else {
        // Fallback on earlier versions
        headView.frame = CGRectMake(0, TopBarHeight+10, SCREENWIDTH, 112+56);
    }
    headView.backgroundColor = TotalWhite;
    [self.view addSubview:headView];
    
    UILabel *editLab = [UILabel new];
    editLab.frame = CGRectMake(30, 19, 120, 16);
    editLab.textColor = titleColor;
    editLab.font = FontMediumWithSize(16);
    editLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:-1 and:@"修改登录手机号"];
    [headView addSubview:editLab];
    
    UIImageView *headIV = [UIImageView new];
    headIV.frame = CGRectMake(SCREENWIDTH-29, 15, 14, 14);
    headIV.image = [UIImage imageNamed:@"shopMore"];
    [headView addSubview:headIV];
    
    UIView *headLine = [UIView new];
    headLine.frame = CGRectMake(15, 55, SCREENWIDTH, 1);
    headLine.backgroundColor = UIColorFromHex(0xF1F1F1);
    [headView addSubview:headLine];
    
    UIButton *editPhoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    editPhoneBtn.frame = CGRectMake(0, 0, SCREENWIDTH, 56);
    [editPhoneBtn addTarget:self action:@selector(editPhone) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:editPhoneBtn];
    
    //清除缓存
    UIImageView *cleanIV = [UIImageView new];
    cleanIV.frame = CGRectMake(15, headLine.bottom+21, 14, 14);
    cleanIV.image = [UIImage imageNamed:@"clean"];
    [headView addSubview:cleanIV];
    
    UILabel *cleanLabel = [UILabel new];
    cleanLabel.frame = CGRectMake(cleanIV.right+11, headLine.bottom+19, 61, 16);
    cleanLabel.textColor = titleColor;
    cleanLabel.font = FontMediumWithSize(16);
    cleanLabel.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:-1 and:@"清理缓存"];
    [headView addSubview:cleanLabel];

    UILabel *numLab = [UILabel new];
    numLab.frame = CGRectMake(SCREENWIDTH-15-100, headLine.bottom+19, 100, 14);
    numLab.textColor = UIColorFromHex(0x999999);
    numLab.font = FontMediumWithSize(14);
    numLab.tag = 15;
    //获取当前缓存
    CacheManager *cachemanager=[CacheManager defaultCacheManager];
    numLab.text= [NSString stringWithFormat:@"%.2fM",[cachemanager GetCacheSize]];
    numLab.textAlignment = NSTextAlignmentRight;
    [headView addSubview:numLab];
    
    UIView *lineV = [UIView new];
    lineV.frame = CGRectMake(15, 55+56, SCREENWIDTH, 1);
    lineV.backgroundColor = UIColorFromHex(0xF1F1F1);
    [headView addSubview:lineV];
    
    UIButton *cleanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cleanBtn.frame = CGRectMake(0, 56, SCREENWIDTH, 55);
    cleanBtn.backgroundColor = ClearColor;
    cleanBtn.tag = 11;
    [cleanBtn addTarget:self action:@selector(cleanBtn:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:cleanBtn];
    
    UIImageView *aboutIV = [UIImageView new];
    aboutIV.frame = CGRectMake(15, lineV.bottom+21, 14, 14);
    aboutIV.image = [UIImage imageNamed:@"about"];
    [headView addSubview:aboutIV];
    
    UILabel *aboutLab = [UILabel new];
    aboutLab.frame = CGRectMake(cleanIV.right+11, lineV.bottom+20, 61, 16);
    aboutLab.textColor = titleColor;
    aboutLab.font = FontMediumWithSize(16);
    aboutLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:-1 and:@"关于异宝"];
    [headView addSubview:aboutLab];
    
    UIImageView *arrowIV = [UIImageView new];
    arrowIV.frame = CGRectMake(SCREENWIDTH-33, lineV.bottom+19, 18, 18);
    arrowIV.image = [UIImage imageNamed:@"shopMore"];
    [headView addSubview:arrowIV];
    
    UILabel *versionLab = [UILabel new];
    versionLab.frame = CGRectMake(SCREENWIDTH-33-100, lineV.bottom+21, 95, 14);
    versionLab.textColor = UIColorFromHex(0x999999);
    versionLab.font = FontMediumWithSize(14);
    //获取当前版本
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    versionLab.text = [NSString stringWithFormat:@"当前版本%@",appVersion];
    versionLab.textAlignment = NSTextAlignmentRight;
    [headView addSubview:versionLab];
    
    UIButton *versionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    versionBtn.frame = CGRectMake(0, lineV.bottom, SCREENWIDTH, 56);
    versionBtn.backgroundColor = ClearColor;
    versionBtn.tag = 12;
    [versionBtn addTarget:self action:@selector(cleanBtn:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:versionBtn];
    
    UIButton *logOut = [UIButton buttonWithType:UIButtonTypeCustom];
    logOut.frame = CGRectMake(0, headView.bottom+10,SCREENWIDTH, 56);
    [logOut setAttributedTitle:[TTKUtil getAtttributeStringWithLineSpace:0 kern:-0.8 and:@"退出登陆"] forState:UIControlStateNormal];
    logOut.backgroundColor = TotalWhite;
    logOut.titleLabel.font = FontMediumWithSize(16);
    [logOut setTitleColor:titleColor forState:UIControlStateNormal];
    [logOut addTarget:self action:@selector(logOutBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:logOut];
}

- (void)editPhone
{
    EditLoginMobileController *editVC = [EditLoginMobileController new];
    [self.navigationController pushViewController:editVC animated:YES];
}

- (void)cleanBtn:(UIButton *)btn
{
    switch (btn.tag) {
        case 11:
        {
            UILabel *label = (UILabel *)[self.view viewWithTag:15];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定清除缓存么？" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                CacheManager *cacheManager = [CacheManager defaultCacheManager];
                [cacheManager clearCache:^(BOOL success)
                 {
                     [cacheManager clearCache:^(BOOL success)
                      {
                          [self ShowHUDWithMessage:@"已全部清除完毕"];
                          label.text = @"0.00M";
                      }];
                 }];
            }];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alert addAction:action2];
            [alert addAction:action1];
            [self presentViewController:alert animated:YES completion:nil];
        }
        case 12:
            
            break;
        default:
            break;
    }
}

- (void)logOutBtn:(UIButton *)btn
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定退出登录么？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:LoginStatue];
        //关闭推送
        [[UIApplication sharedApplication] unregisterForRemoteNotifications];
        //删除别名
        [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            NSLog(@"iResCode = %li,iAlias=%@,seq=%li",(long)iResCode,iAlias,seq);
            LoginController *signVC = [LoginController new];
            AppDelegate* app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            app.window.rootViewController =[[UINavigationController alloc]initWithRootViewController:signVC];
        } seq:0];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action2];
    [alert addAction:action1];
    [self presentViewController:alert animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
